package engine.system;

import java.io.Serializable;

public class Timer implements Serializable{

	private static final long serialVersionUID = 1L;

	public static final int EVENT_TIMEOUT = 500 ; 
	
	private long lastTimeMeasured = 0 ; 
	private long pauseTime = 0 ; 
		
	public Timer(){
		
	}
	
	public long getLastTimeMeasured() {
		return lastTimeMeasured;
	}

	private void setLastTimeMeasured(long lastTimeMeasured) {
		this.lastTimeMeasured = lastTimeMeasured;
	}

	public void delay(long msec){
		try {
			long ftc = System.currentTimeMillis(); 
			while((System.currentTimeMillis() - ftc) < msec){
				Thread.currentThread(); 
				Thread.sleep(0); 
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
	}
	
	@SuppressWarnings("static-access")
	public void delayThread(Thread thread, long msec){
		try {
			long ftc = System.currentTimeMillis(); 
			while((System.currentTimeMillis() - ftc) < msec)
				thread.sleep(0); 
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
	}
	
	public void start(){
		this.setLastTimeMeasured(System.currentTimeMillis()); 
	}
	
	public void pause(){
		pauseTime = System.currentTimeMillis(); 
	}
	
	public void continueTimer(){
		long delta = System.currentTimeMillis() - pauseTime ; 
		lastTimeMeasured += delta ;
	}
	
	public long getAbsoluteDelta(){
		long result = System.currentTimeMillis() - lastTimeMeasured;
		return result ; 
	} 
	
	public long getRelativeDelta(){
		long result = System.currentTimeMillis() - lastTimeMeasured;
		lastTimeMeasured = System.currentTimeMillis(); 
		return result ; 
	} 
			
	public long getDeltaFrom(long start){
		return System.currentTimeMillis() - start ; 
	}	

}
